package com.jilian.mydemo.modul.my.adapter;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jilian.mydemo.R;
import com.jilian.mydemo.common.dto.FinishImgDto;
import com.jilian.mydemo.common.dto.ProductDetailDto;
import com.jilian.mydemo.common.dto.VideoArticlesDto;
import com.jilian.mydemo.listener.CustomItemClickListener;
import com.jilian.mydemo.modul.order.adapter.AccessoriesAdapter;
import com.jilian.mydemo.modul.order.ui.InputServiceDataActivity;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.utils.NumberUtils;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.service.DActivity;


public class InputServiceDataAdapter extends RecyclerView.Adapter<InputServiceDataAdapter.ViewHolder> implements CustomItemClickListener {
    private InputServiceDataActivity mContext;
    private List<FinishImgDto> datas;

    public List<FinishImgDto> getDatas() {
        return datas;
    }

    public void setDatas(List<FinishImgDto> datas) {
        this.datas = datas;
    }

    private CustomItemClickListener listener;

    private InputServiceListener inputServiceListener;

    public InputServiceDataAdapter(InputServiceDataActivity context, List<FinishImgDto> datas, CustomItemClickListener listener, InputServiceListener inputServiceListener) {
        mContext = context;
        this.datas = datas;
        this.listener = listener;
        this.inputServiceListener = inputServiceListener;

    }

    public interface InputServiceListener {
        /**
         * 添加设备
         *
         * @param position
         */
        void addEq(int position);

        /**
         * @param position 安装位置
         */
        void etInstallationPosition(int position);

        /**
         * 安装管路
         *
         * @param position
         */
        void etInstallationPiping(int position);

        /**
         * 安装结构
         *
         * @param position
         */
        void etInstallationStructure(int position);

        /**
         * 用途
         *
         * @param position
         */
        void etUse(int position);

        /**
         * 选择生产日期
         *
         * @param position
         */
        void etProductDate(int position);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_input_servicedata, parent, false);
        ViewHolder viewHolder = new ViewHolder(view, listener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        AccessoriesAdapter adapter = new AccessoriesAdapter(mContext, datas.get(position).getLists(), this,this);
        holder.recyclerView.setLayoutManager(linearLayoutManager);
        holder.recyclerView.setAdapter(adapter);
        holder.rlAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inputServiceListener.addEq(position);
            }
        });
        holder.tvProductPosition.setText("服务产品信息" + (position + 1));
        //产品序列ID
        holder.etId.setText(datas.get(position).getSelldetailId());
        //产品编号
        holder.etProductNo.setText(datas.get(position).getProductCode());
        //产品名称
        holder.etProductName.setText(datas.get(position).getProductName());
        String date = datas.get(position).getProduceTime();
        if (!TextUtils.isEmpty(date) && date.contains("00:00:00")) {
            String dateStr = date.replace("00:00:00", "");
            //生产日期
            holder.etProductDate.setText(dateStr);
        } else {
            //生产日期
            holder.etProductDate.setText(date);
        }

        //机身编码
        holder.etEqName.setText(datas.get(position).getSerialNum());
        holder.etEqName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                datas.get(position).setSerialNum(holder.etEqName.getText().toString());
            }
        });


        //保修卡号
        holder.etCardNo.setText(datas.get(position).getRepairNum());
        holder.etCardNo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                datas.get(position).setRepairNum(holder.etCardNo.getText().toString());
            }
        });


        //滤芯编号
        holder.etFilterNo.setText(datas.get(position).getLvXinCode());

        holder.etFilterNo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                datas.get(position).setLvXinCode(holder.etFilterNo.getText().toString());
            }
        });


        //请输入原水压
        holder.etOriginalWaterPressure.setText(datas.get(position).getOldWaterGage());
        //用户自己输入
        holder.etOriginalWaterPressure.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                datas.get(position).setOldWaterGage(holder.etOriginalWaterPressure.getText().toString());
            }
        });

        //请输入新水压
        holder.etNewWaterPressure.setText(datas.get(position).getNewWaterGage());
        //用户自己输入
        holder.etNewWaterPressure.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                datas.get(position).setNewWaterGage(holder.etNewWaterPressure.getText().toString());
            }
        });


        //请输入安装位置
        holder.etInstallationPosition.setText(datas.get(position).getFixPlaceName());


        //安装管路
        holder.etInstallationPiping.setText(datas.get(position).getFixPipeName());


        //安装结构
        holder.etInstallationStructure.setText(datas.get(position).getFixFrameName());


        //用途
        holder.etUse.setText(datas.get(position).getPurposeName());


        /**
         * 请输入安装位置
         */
        holder.etInstallationPosition.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inputServiceListener.etInstallationPosition(position);
            }
        });

        /**
         * 安装管路
         */
        holder.etInstallationPiping.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inputServiceListener.etInstallationPiping(position);
            }
        });
        /**
         * 选择安装结构
         */
        holder.etInstallationStructure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inputServiceListener.etInstallationStructure(position);
            }
        });

        /**
         * 用途
         */
        holder.etUse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inputServiceListener.etUse(position);
            }
        });
        holder.etProductDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inputServiceListener.etProductDate(position);
            }
        });
        if (EmptyUtils.isNotEmpty(datas.get(position).getLists())) {
            holder.rlAllPrice.setVisibility(View.VISIBLE);
            holder.tvAllPrice.setText("￥" + getAllPrice(datas.get(position).getLists()));
        } else {
            holder.rlAllPrice.setVisibility(View.GONE);
        }


    }

    /**
     * 获取总价格
     *
     * @param lists
     * @return
     */
    private String getAllPrice(List<ProductDetailDto> lists) {
        double allPrice = 0;
        for (int i = 0; i < lists.size(); i++) {
            allPrice += Double.parseDouble(lists.get(i).getActualPrice())*Integer.parseInt(lists.get(i).getQuantity());
        }
        return NumberUtils.forMatNumber(allPrice);
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    @Override
    public void onItemClick(View view, int position) {


    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout rlAllPrice;


        private TextView etId;
        private TextView etProductNo;
        private TextView etProductName;
        private TextView etProductDate;
        private EditText etEqName;
        private EditText etCardNo;
        private EditText etFilterNo;
        private EditText etOriginalWaterPressure;
        private EditText etNewWaterPressure;
        private TextView etInstallationPosition;
        private TextView etInstallationPiping;
        private TextView etInstallationStructure;
        private TextView etUse;
        private RecyclerView recyclerView;
        private RelativeLayout rlAdd;
        private TextView tvProductPosition;
        private TextView tvAllPrice;


        public ViewHolder(final View itemView, final CustomItemClickListener listener) {
            super(itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onItemClick(itemView, getAdapterPosition());
                }
            });
            rlAllPrice = (RelativeLayout) itemView.findViewById(R.id.rl_all_price);
            tvAllPrice = (TextView) itemView.findViewById(R.id.tv_all_price);
            tvProductPosition = (TextView) itemView.findViewById(R.id.tv_product_position);
            etId = (TextView) itemView.findViewById(R.id.et_id);
            etProductNo = (TextView) itemView.findViewById(R.id.et_product_no);
            etProductName = (TextView) itemView.findViewById(R.id.et_product_name);
            ;
            etEqName = (EditText) itemView.findViewById(R.id.et_eq_name);
            etCardNo = (EditText) itemView.findViewById(R.id.et_card_no);
            etFilterNo = (EditText) itemView.findViewById(R.id.et_filter_no);
            etOriginalWaterPressure = (EditText) itemView.findViewById(R.id.et_original_water_pressure);
            etNewWaterPressure = (EditText) itemView.findViewById(R.id.et_new_water_pressure);
            etInstallationPosition = (TextView) itemView.findViewById(R.id.et_installation_position);
            etInstallationPiping = (TextView) itemView.findViewById(R.id.et_installation_piping);
            etInstallationStructure = (TextView) itemView.findViewById(R.id.et_installation_structure);
            etUse = (TextView) itemView.findViewById(R.id.et_use);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.recyclerView);
            rlAdd = (RelativeLayout) itemView.findViewById(R.id.rl_add);
            etProductDate = (TextView) itemView.findViewById(R.id.et_product_date);

        }

    }
}
